﻿using System;
using HU8.UCenter.Plugins.URating.DAL;
using Lbb.URating.DL;
using Lbb.Configuration;
using Lbb.Web.Mvc;

namespace Lbb.URating.Controllers
{
    /// <summary>
    /// 评分控制器基类
    /// </summary>
    /// <author>Chiron</author>
    public abstract class URatingControllerBase : LbbControllerBase
    {
        public const string ConfigPreKey = "urating";
        #region DB
        /// <summary>
        /// 数据源
        /// </summary>
        public URatingDataContext DB
        {
            get
            {
                if (_db == null)
                {
                    var connName = ConfigHelper.GetAppSettingValue(ConfigPreKey, "connName");
                    if (string.IsNullOrEmpty(connName)) throw new Exception("请配置评分系统的数据库连接串(urating:connName)!");
                    var connStr = ConfigHelper.GetConnectionString(connName);
                    _db = new URatingDataContext(connStr);
                    if (!_db.DatabaseExists())
                    {
                        _db.CreateDatabase();
                    }
                }
                return _db;
            }
        }
        private URatingDataContext _db;
        /// <summary>
        /// 评分分类操作库
        /// </summary>
        public U_ClickCategoryRepository U_ClickCategoryRepository
        {
            get
            {
                return _u_ClickCategoryRepository ?? (_u_ClickCategoryRepository = new U_ClickCategoryRepository(DB));
            }
        }
        private U_ClickCategoryRepository _u_ClickCategoryRepository;
        /// <summary>
        /// 点击器操作库
        /// </summary>
        public U_ClickRepository U_ClickRepository
        {
            get
            {
                return _u_ClickRepository ?? (_u_ClickRepository = new U_ClickRepository(DB));
            }
        }
        private U_ClickRepository _u_ClickRepository;
        /// <summary>
        /// 评分项操作库
        /// </summary>
        public U_ClickRatingRepository U_ClickRatingRepository
        {
            get
            {
                return _u_ClickRatingRepository ?? (_u_ClickRatingRepository = new U_ClickRatingRepository(DB));
            }
        }
        private U_ClickRatingRepository _u_ClickRatingRepository;
        /// <summary>
        /// 用户评分操作库
        /// </summary>
        public U_RatingRepository U_RatingRepository
        {
            get
            {
                return _u_RatingRepository ?? (_u_RatingRepository = new U_RatingRepository(DB));
            }
        }
        private U_RatingRepository _u_RatingRepository;
        #endregion DB
    }
}
